//
//  JiaoCueZuoYeCollectionView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/5/9.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "JiaoCueZuoYeCollectionView.h"
#import "TWorkListVC.h"
#import "SYRequestAPI+Teach.h"
#import "TeacherClassListModel.h"
#import "JiaoXueZuoYeLieBiaoModel.h"
#import "CHClassTypeSelectedView.h"
#import "SYKaoShiTongJiView.h"
#import "SYYueJuanModel.h"
#import "KaoPingZuoYeCell.h"
#import "SYZuoYeTongJiView.h"
#define CellSP 20*fw
#define CellW 30*fw


@interface JiaoCueZuoYeCollectionView ()<TWorkSelectedTopViewDelegate>
{
    TWorkListVC *workListView;
    BOOL Type;
    TeacherClassListModel *mainClassModel;
    NSString *getParamaOfDate;//全局时间搜索字段
    int pageIndex;
    int pageSize;
}
@property (nonatomic, strong)NSMutableArray *workList;
@property (nonatomic, assign) float mainw;

@property (strong, nonatomic) SYKaoShiTongJiView *kaoView;//考试通几

@property (strong, nonatomic) SYZuoYeTongJiView *zuoView;//作业通几

@end

@implementation JiaoCueZuoYeCollectionView

- (TWorkSelectedTopView *)topWorkView{
    if (!_topWorkView) {
        TWorkSelectedTopView *topView = [[TWorkSelectedTopView alloc]init];
        topView.frame = CGRectMake(0, 0, 875*fw, 44);
        topView.delegate = self;
        _topWorkView = topView;
    }
    return _topWorkView;
}
- (UILabel *)noDataLabel{
    if (!_noDataLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"当前数据为空";
        label.textAlignment = 1;
        _noDataLabel = label;
    }
    return _noDataLabel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)initSubView{
    Type = NO;
    pageSize = 30;
    pageIndex = 1;
    getParamaOfDate = @"0";
    _mainw = MAINW;
    _workList = [NSMutableArray new];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showView) name:@"ShowView" object:nil];
    [self.collectionView registerNib:[UINib nibWithNibName:@"KaoPingZuoYeCell" bundle:nil] forCellWithReuseIdentifier:@"KaoPingZuoYeCell"];
//    [self.collectionView registerClass:[TCollectionViewCell class] forCellWithReuseIdentifier:@"TCollectionViewCell"];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"ChangeCellSpaNotice" object:nil];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.topWorkView];
    [self.topWorkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@44);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0).offset(-20);
    }];
    [self.view addSubview:self.noDataLabel];
    [_noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@44);
        make.bottom.equalTo(@0);
    }];
    [self defalutClassID];
    
    [self.collectionView headerRefreshBlock:^{
        self->pageIndex = 1;
        [self->_workList removeAllObjects];
        [self getZuoYeRequest:@""];
    }];
//    [self.collectionView fooderRefreshBlock:^{
//        [self getZuoYeRequest:@""];
//    }];
}

#pragma mark - 默认显示的班级
- (void)defalutClassID{
    NSMutableArray *array = [PublickFunctionObject getArrayForDataForKey:ClassListOfTeacherDefalut];
    TeacherClassListModel *model = [TeacherClassListModel yy_modelWithJSON:array[0]];
    model.id = @"0";
    mainClassModel = model;
    [self getZuoYeRequest:@""];
    
}
//- (void)showView{
// 
//}
//- (void)changeView:(NSNotification *)info{
//
//    BOOL type = [[info.userInfo objectForKey:@"Type"] intValue];
//    Type = type;
//    [self.collectionView reloadData];
//    _mainw = [PublickFunctionObject currentScreenSize:type];
//    workListView.mainScrollView.frame = CGRectMake(0, 44, _mainw, self.view.frame.size.height-84);
//    workListView.mainScrollView.contentSize = CGSizeMake(_mainw*3, 0);
//    workListView.SJXQView.frame = CGRectMake(0, 0, _mainw, self.view.frame.size.height);
//    workListView.RSView.frame = CGRectMake(_mainw, 0, _mainw, self.view.frame.size.height);
//    workListView.WPView.frame = CGRectMake(_mainw*2, 0, _mainw, self.view.frame.size.height);
//    [workListView.SJXQView.tableList reloadData];
//    [workListView.RSView.tableList reloadData];
//
//    if (workListView.lookDetailView) {
//        workListView.lookDetailView.frame = CGRectMake(0, 0, _mainw, self.view.frame.size.height);
//    }
//    if (workListView.submitDetailView) {
//        workListView.submitDetailView.frame = CGRectMake(0, 0, _mainw, self.view.frame.size.height);
//    }
//    [workListView.navView reloadInputViews];
//    NSLog(@"宽度标记  %f",_mainw);
//    [MBProgressHUD mj_showSuccess:@"调整中。。。" toView:self.view];
//    dispatch_async(dispatch_queue_create(0, 0), ^{
//        // 子线程执行任务（比如获取较大数据）
//
//        dispatch_async(dispatch_get_main_queue(), ^{
//            // 通知主线程刷新 神马的
//            [self->workListView.WPView.tableList reloadData];
//            [MBProgressHUD mj_hideHUDForView:self.view];
//        });
//    });
//
//}
- (void)leftWorkButtonClick{
    __block JiaoCueZuoYeCollectionView *mySelf = self;
//    [CJYearMonthSelectedView showDatePickerWithTitle:@"选择月份" minDateStr:@"2000-01" resultBlock:^(NSString *selectValue) {
//        //选择完成后的操作
//        self->pageIndex = 1;
//        [self->_workList removeAllObjects];
//        NSArray *array = [selectValue componentsSeparatedByString:@"-"];
//        mySelf.topWorkView.timeButton.bottomLabel.text = [NSString stringWithFormat:@"%@年%@月",array[0],array[1]];
//        self->getParamaOfDate = selectValue;
//       // NSLog(@"selected month is %@", selectValue);
//        [mySelf getZuoYeRequest:selectValue];
//    }];     
    [CHClassTypeSelectedView showDatePickerWithTitle:@"选择测试类型" minDateStr:@"测试" resultBlock:^(TeacherClassListModel * _Nonnull selectValue) {
        
        self->getParamaOfDate = [NSString stringWithFormat:@"%@",selectValue.status];
        self->pageIndex = 1;
        [self->_workList removeAllObjects];
        [self getZuoYeRequest:@""];
        self.topWorkView.timeButton.bottomLabel.text = selectValue.name;
    }];
}
- (void)rightWorkButtonClick{
    [CJClassSelectedView showDatePickerWithTitle:@"选择班级" minDateStr:@"年级一" resultBlock:^(TeacherClassListModel * _Nonnull selectValue) {
        //self->_topWorkView.nianjiButton.bottomLabel.text = selectValue.name;
        self->mainClassModel = selectValue;
        self->pageIndex = 1;
        [self->_workList removeAllObjects];
        [self getZuoYeRequest:@""];
        self.topWorkView.nianjiButton.bottomLabel.text = selectValue.name;
    }];
}
#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  
    return _workList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KaoPingZuoYeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KaoPingZuoYeCell" forIndexPath:indexPath];
    if (_workList.count >0) {
        JiaoXueZuoYeLieBiaoModel *model = _workList[indexPath.row];
//        cell.chuQingLabel.hidden = YES;
//        [cell setTitleAtrr:[NSString stringWithFormat:@"%@",model.lessonName]];
//        [cell setMentorTeacherAtrr:model.paperName];
//        [cell setAddressLabelAtrr:model.createDate];
//        [cell setTimeLabelAtrr:model.createDate];
        cell.titleLable.text = [NSString stringWithFormat:@"%@",model.lessonName];
        cell.classLable.text = model.paperName;
        cell.timeLable.text = model.createDate;
        cell.typeLable.text = model.templateTypeName;
        cell.className.text = model.className;
    }
    
    return cell;
}
#pragma mark == collectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(204*fw, 145*fh);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    //top, left, bottom, right
    if (Type == YES) {
        return UIEdgeInsetsMake(CellSP, CellSP, CellSP,CellSP);
    }else{
        return UIEdgeInsetsMake(CellSP, CellW, CellSP,CellW);
    }
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return CellSP;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return CellSP;
}

//是否允许移动Item
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0)
{
    return YES;
}
//移动Item时触发的方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);
{
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        //作业
//    JiaoXueZuoYeLieBiaoModel *model = _workList[indexPath.row];
//    workListView = [[TWorkListVC  alloc]initWithWight:_mainw andClassID:model.classId andPaperID:model.paperId];
//    workListView.view.backgroundColor = MAINBACKCOLOR;
//    [self.view addSubview:workListView.view];
//    workListView.SJXQView.model = _workList[indexPath.row];
//    [workListView.SJXQView setValueForHeader];
////    workListView.WPView.headerModel = _workList[indexPath.row];
////    [workListView.WPView setValueForHeader];
//    [workListView.view mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@0);
//        make.right.equalTo(@0);
//        make.top.equalTo(@0);
//        make.bottom.equalTo(@0);
//    }];
//    [MBProgressHUD mj_hideHUDForView:self.view];
//    [PublickFunctionObject changeMainView:@"TeachChangeNotice" AndType:Type];
    
    JiaoXueZuoYeLieBiaoModel *listModel = _workList[indexPath.row];
    
//    [MBProgressHUD mj_showMessage:nil toView:self];
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI yj_requestNewStatisticsServiceEXamInfoTemplateId:listModel.paperId WithClassId:listModel.classId success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        SYExamInfoModel *infoModel = nil;
        if (dataListArr.count > 0) {
            infoModel = dataListArr.firstObject;
            infoModel.templateId = listModel.paperId;
        }
        if ([listModel.templateTypeId integerValue] == 5) {
            
            self->_zuoView = [[SYZuoYeTongJiView alloc] init];
            weakSelf.zuoView.type = listModel.templateTypeId;
            [weakSelf.view addSubview:weakSelf.zuoView];
            [weakSelf.zuoView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.top.equalTo(weakSelf.view);
            }];
            [weakSelf.zuoView requestDataListModel:infoModel];

        
        }else{
            self->_kaoView = [[SYKaoShiTongJiView alloc] init];
            //考试统计
            weakSelf.kaoView.type = listModel.templateTypeId;
            weakSelf.kaoView.scheduleId = listModel.scheduleId;
            [weakSelf.view addSubview:weakSelf.kaoView];
            [weakSelf.kaoView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.top.equalTo(weakSelf.view);
            }];
            [weakSelf.kaoView requestDataListModel:infoModel];
        }

        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        [MBProgressHUD zr_showError:error.message];
    }];
    
    
}
- (void)goWorkDetailView:(TheNewWorkListModel *)model{
    JiaoXueZuoYeLieBiaoModel *mo = [JiaoXueZuoYeLieBiaoModel new];
    mo.classId = model.classId;
    mo.className = model.className;
    mo.lessonId = model.lessonId;
    mo.lessonName = model.lessonName;
    mo.paperId = model.paperId;
    mo.paperName = model.paperName;
    mo.handStudents = model.submitStudents;
    mo.totalStudents = model.classStudents;
    mo.status  = @"";
    mo.createDate = model.upLoadDate;
    workListView = [[TWorkListVC  alloc]initWithWight:_mainw andClassID:model.classId andPaperID:model.paperId];
    workListView.view.backgroundColor = MAINBACKCOLOR;
    [self.view addSubview:workListView.view];
    workListView.SJXQView.model = mo;
    [workListView.SJXQView setValueForHeader];
//    workListView.WPView.headerModel = mo;
//    [workListView.WPView setValueForHeader];
    [workListView.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [MBProgressHUD mj_hideHUDForView:self.view];
}
#pragma mark <UICollectionViewDelegate>

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    
}
- (void)getDataForView:(NSString *)name{
    [self.collectionView reloadData];
}
- (void)dealloc{
//    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"ShowView" object:nil];
}
#pragma mark - 教学-作业
- (void)getZuoYeRequest:(NSString *)data{
    if (mainClassModel.id.length == 0) {
        mainClassModel = [TeacherClassListModel new];
        mainClassModel.id = @"0";
    }
//http://apipad.31waiyu.com/paper/CorrectService.svc/CorrectLog
//    [MBProgressHUD mj_showMessage:nil toView:self.collectionView];
    NSDictionary *parama = @{@"classId":mainClassModel.id,
                             @"lessonId":@"0",
                             @"date":@"",
                             @"templateTypeId":getParamaOfDate,
                             @"pageIndex":[NSString stringWithFormat:@"%d",pageIndex],
                             @"pageSize":[NSString stringWithFormat:@"%d",10000]};
   // NSLog(@"s教学-作业 %@",parama);
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI sy_getHomeWordServiceHomeWorKList:parama success:^(id  _Nonnull responseObject) {
       // NSLog(@"作业列表 %@",responseObject);
        [MBProgressHUD mj_hideHUD];
        NSArray *array = [responseObject objectForKey:@"data"];
        if (array.count>0) {
            self->pageIndex ++;
            self->_noDataLabel.hidden = YES;
        }
        for (NSDictionary *dic in array) {
            JiaoXueZuoYeLieBiaoModel *model = [JiaoXueZuoYeLieBiaoModel yy_modelWithJSON:dic];
            [self->_workList addObject:model];
        }
        if (self->_workList.count == 0) {
            self->_noDataLabel.hidden = NO;
        }
        [self.collectionView reloadData];
//        [MBProgressHUD mj_hideHUDForView:self.collectionView];
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
//        [MBProgressHUD mj_hideHUDForView:self.collectionView];
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
    }];
    
    [SYRequestAPI postDataFromURLScheme:@"common/CommonService.svc/GetTemplateTypeList" parameterDictionary:nil success:^(id responseObject) {
        
    } fail:^(NSError *error) {
        
    }];
}
- (void)reloadView{
    [_workList removeAllObjects];
    pageIndex = 1;
    [self getZuoYeRequest:@""];
}

//- (SYKaoShiTongJiView *)kaoView {
//    if (!_kaoView) {
//        _kaoView = [[SYKaoShiTongJiView alloc] init];
//    }
//    return _kaoView;
//}

@end
